package com.cyjt.operation.ui;

import java.util.ArrayList;
import java.util.List;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.ViewPagerAdaper;
import com.cyjt.operation.fragment.DynamicNodesStatusFragment;
import com.cyjt.operation.fragment.StaticNodesStatusFragment;
import com.cyjt.operation.uitools.ExampleUtil;
import com.cyjt.operation.view.ActionBarLayout;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 节点状态列表的Activity（包括静态节点状态列表的Fragment与动态节点状态列表的Fragment）
 * 
 * @author LTP
 * 
 */
public class NodesStatusListActivity extends FragmentActivity {
	private String TAG = "NodesStatusListActivity";
	/** ViewPager的Fragment集合 */
	private List<Fragment> fragmentList = new ArrayList<Fragment>();
	private ViewPager mViewPager;
	/** ViewPager中切换静态节点与动态节点的Tab */
	private TextView textView_static_nodes_status_switch, textView_dynamic_nodes_status_switch;
	/** 切换正异常节点的Button */
	private static Button button_node_status_switch;
	/** 静态节点状态列表的Fragment */
	private StaticNodesStatusFragment mStaticNodesStatusFragment;
	/** 动态节点状态列表的Fragment */
	private DynamicNodesStatusFragment mDynamicNodesStatusFragment;

	public static boolean isForeground = false;
	/** 震动传感器 */
	private Vibrator mVibrator;
	private final int ROCKPOWER = 18;// 这是传感器系数
	private SensorManager mSensorManager;
	private MediaPlayer player, player_false, player_true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nodes_status_list);
		initView();
		initViewPager();
		registerMessageReceiver();
		// 获取传感器管理服务
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		// 震动服务
		mVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		player_false = MediaPlayer.create(this, R.raw.music_false);
		player_true = MediaPlayer.create(this, R.raw.music_true);
		player = MediaPlayer.create(this, R.raw.music);
	}

	/**
	 * 控件初始化
	 */
	private void initView() {
		ActionBarLayout.setTitle("节点列表");
		ActionBarLayout.isShowProgressBar(true);
		mStaticNodesStatusFragment = new StaticNodesStatusFragment();
		mDynamicNodesStatusFragment = new DynamicNodesStatusFragment();
		fragmentList.add(mStaticNodesStatusFragment);
		fragmentList.add(mDynamicNodesStatusFragment);
		textView_static_nodes_status_switch = (TextView) findViewById(R.id.textView_static_nodes_status);
		textView_static_nodes_status_switch.setOnClickListener(new MyOnClickListener(0));
		textView_dynamic_nodes_status_switch = (TextView) findViewById(R.id.textView_dynamic_nodes_status);
		textView_dynamic_nodes_status_switch.setOnClickListener(new MyOnClickListener(1));
		button_node_status_switch = (Button) findViewById(R.id.button_node_status_switch);
		button_node_status_switch.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO 切换节点正异常状态列表的Button的点击事件
				if ("查看正常节点".equals(button_node_status_switch.getText().toString())) {
					mStaticNodesStatusFragment.loadNomalStaticNodesStatusList();
					mDynamicNodesStatusFragment.loadNomalDynamicNodesStatusList();
					button_node_status_switch.setText("查看异常节点");
				} else {
					mStaticNodesStatusFragment.loadAbnormalStaticNodesStatusListData();
					mDynamicNodesStatusFragment.loadAbnormalDynamicNodesStatusList();
					button_node_status_switch.setText("查看正常节点");
				}
			}
		});
	}

	/**
	 * 初始化ViewPager
	 */
	private void initViewPager() {
		mViewPager = (ViewPager) findViewById(R.id.viewPager_for_nodes_status);
		ViewPagerAdaper mViewPagerAdaper = new ViewPagerAdaper(getSupportFragmentManager(), fragmentList);
		mViewPager.setAdapter(mViewPagerAdaper);
		mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO 页面切换的回调
				switch (position) {
				case 0:
					Log.d(TAG, "我是静态节点页面");
					textView_static_nodes_status_switch.setBackgroundResource(R.drawable.collect_tab_left_btn_sel);
					textView_static_nodes_status_switch.setTextColor(getResources().getColor(R.color.white));
					textView_dynamic_nodes_status_switch.setBackgroundResource(R.drawable.collect_tab_right_btn);
					textView_dynamic_nodes_status_switch.setTextColor(getResources().getColor(R.color.black));
					break;
				case 1:
					Log.d(TAG, "我是动态节点页面");
					textView_static_nodes_status_switch.setBackgroundResource(R.drawable.collect_tab_left_btn);
					textView_static_nodes_status_switch.setTextColor(getResources().getColor(R.color.black));
					textView_dynamic_nodes_status_switch.setBackgroundResource(R.drawable.collect_tab_right_btn_sel);
					textView_dynamic_nodes_status_switch.setTextColor(getResources().getColor(R.color.white));
					break;
				default:
					break;
				}
			}

			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub

			}
		});
	}

	/**
	 * 设置ViewPager自定义标题文本的点击事件
	 * 
	 * @author LTP
	 */
	private class MyOnClickListener implements OnClickListener {
		private int index = 0;

		public MyOnClickListener(int i) {
			index = i;
		}

		@Override
		public void onClick(View v) {
			// 设置点击后跳转的选项卡
			mViewPager.setCurrentItem(index);
		}
	}

	/**
	 * 给静态节点的Fragment和动态节点的Fragment提供获取按键文本的静态方法
	 * 
	 * @return
	 */
	public static String getButtonText() {
		return button_node_status_switch.getText().toString();
	}

	// for receive customer msg from jpush server
	private MessageReceiver mMessageReceiver;
	public static final String MESSAGE_RECEIVED_ACTION = "com.cyjt.operation.MESSAGE_RECEIVED_ACTION";
	public static final String KEY_TITLE = "title";
	public static final String KEY_MESSAGE = "message";
	public static final String KEY_EXTRAS = "extras";

	public void registerMessageReceiver() {
		mMessageReceiver = new MessageReceiver();
		IntentFilter filter = new IntentFilter();
		filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
		filter.addAction(MESSAGE_RECEIVED_ACTION);
		registerReceiver(mMessageReceiver, filter);
	}

	public class MessageReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
				String messge = intent.getStringExtra(KEY_MESSAGE);
				String extras = intent.getStringExtra(KEY_EXTRAS);
				StringBuilder showMsg = new StringBuilder();
				showMsg.append(KEY_MESSAGE + " : " + messge);
				if (!ExampleUtil.isEmpty(extras)) {
					showMsg.append(KEY_EXTRAS + " : " + extras);
				}
				Toast.makeText(NodesStatusListActivity.this, showMsg.toString(), Toast.LENGTH_LONG).show();
				mStaticNodesStatusFragment.feachStaticNodesStatus();
				mVibrator.vibrate(2000);
				player_false = MediaPlayer.create(NodesStatusListActivity.this, R.raw.music_false);
				player_false.start();

			}
		}
	}

	@Override
	protected void onResume() {
		isForeground = true;
		// 震动服务
		mVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		super.onResume();
	}

	@Override
	protected void onPause() {
		isForeground = false;
		mVibrator.cancel();
		player_false.stop();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		unregisterReceiver(mMessageReceiver);
		super.onDestroy();
	}
}